<template>
  <div
    :class="[
      'detail-list',
      size === 'small' ? 'small' : 'large',
      layout === 'vertical' ? 'vertical' : 'horizontal',
    ]"
  >
    <div v-if="title" class="title">{{ title }}</div>
    <a-row>
      <slot></slot>
    </a-row>
  </div>
</template>

<script>
  import ACol from 'ant-design-vue/es/grid/Col'
  const Item = {
    name: 'DetailListItem',
    props: {
      term: {
        type: String,
        required: false,
      },
    },
    inject: {
      col: {
        type: Number,
      },
    },
    methods: {
      renderTerm(h, term) {
        return term
          ? h(
              'div',
              {
                attrs: {
                  class: 'term',
                },
              },
              [term]
            )
          : null
      },
      renderContent(h, content) {
        return h(
          'div',
          {
            attrs: {
              class: 'content',
            },
          },
          [content]
        )
      },
    },
    render(h) {
      const term = this.renderTerm(h, this.$props.term)
      const content = this.renderContent(h, this.$slots.default)
      return h(
        ACol,
        {
          props: responsive[this.col],
        },
        [term, content]
      )
    },
  }

  const responsive = {
    1: { xs: 24 },
    2: { xs: 24, sm: 12 },
    3: { xs: 24, sm: 12, md: 8 },
    4: { xs: 24, sm: 12, md: 6 },
  }

  export default {
    name: 'DetailList',
    Item: Item,
    props: {
      title: {
        type: String,
        required: false,
      },
      col: {
        type: Number,
        required: false,
        default: 3,
      },
      size: {
        type: String,
        required: false,
        default: 'large',
      },
      layout: {
        type: String,
        required: false,
        default: 'horizontal',
      },
    },
    provide() {
      return {
        col: this.col > 4 ? 4 : this.col,
      }
    },
  }
</script>

<style lang="less">
  .detail-list {
    .title {
      margin-bottom: 16px;
      font-size: 16px;
      font-weight: bolder;
      color: @title-color;
    }
    .term {
      display: table-cell;
      padding-bottom: 16px;
      margin-right: 8px;
      // Line-height is 22px IE dom height will calculate error
      line-height: 20px;
      color: @title-color;
      white-space: nowrap;
      &::after {
        position: relative;
        top: -0.5px;
        margin: 0 8px 0 2px;
        content: ':';
      }
    }
    .content {
      display: table-cell;
      width: 100%;
      padding-bottom: 16px;
      line-height: 22px;
      color: @text-color;
    }
    &.small {
      .title {
        margin-bottom: 12px;
        font-size: 14px;
        font-weight: normal;
        color: @text-color;
      }
      .term,
      .content {
        padding-bottom: 8px;
      }
    }
    &.large {
      .term,
      .content {
        padding-bottom: 16px;
      }
    }
    &.vertical {
      .term {
        padding-bottom: 8px;
      }
      .term,
      .content {
        display: block;
      }
    }
  }
</style>
